package cn.yusei.teamTribe.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.MessageType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.entity.Message;
import cn.yusei.teamTribe.service.MessageService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	@RequestMapping(value="/message/get")
	public void get(@RequestAttribute int id, @RequestParam int type, PrintWriter out)
	{
		if(!MessageType.isMessageType(type))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		List<Message> messages = null;
		try
		{
			messages= messageService.getMessage(id, type);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("message", messages);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
}
